# -*- coding: utf-8 -*-
import logging
import opentracing
import opentracing.span
from .constants import NOOP_SPAN_ID

LOG_LEVEL_DBG = logging.getLevelName(logging.DEBUG)
LOG_LEVEL_INFO = logging.getLevelName(logging.INFO)
LOG_LEVEL_WARN = logging.getLevelName(logging.WARNING)
LOG_LEVEL_ERROR = logging.getLevelName(logging.ERROR)
LOG_LEVEL_FAULT = logging.getLevelName(logging.FATAL)
LOG_LEVEL_EXCEPTION = 'EXCEPTION'

class LogSpan(opentracing.Span):
    """description of class"""
    def __init__(self, context, tracer):
        opentracing.Span.__init__(self, tracer, context)
        self.ring = []
        
    @property
    def is_noop(self):
        return True
        
    @property
    def span_id(self):
        return NOOP_SPAN_ID
        
    def logger(self,log_ring=[], need_record_where_it_is=False):

        class span_logger:
            def __init__(self, ring, log_handle):
                self.log_ring = ring
                self.log = log_handle
                
            def get_msg(self,msg,*args):
                if args:
                    new_msg = msg % args
                else:
                    new_msg = msg
                return new_msg

            def debug(self, msg, *args):
                self.log(self.log_ring, LOG_LEVEL_DBG,self.get_msg(msg, *args))

            def info(self, msg, *args):
                self.log(self.log_ring, LOG_LEVEL_INFO,self.get_msg(msg, *args))

            def warn(self, msg, *args):
                self.log(self.log_ring, LOG_LEVEL_WARN,self.get_msg(msg, *args),need_record_where_it_is)

            def error(self, msg, *args):
                self.log(self.log_ring, LOG_LEVEL_ERROR,self.get_msg(msg, *args),need_record_where_it_is)
                
            def exception(self, msg='', *args):
                self.log(self.log_ring, LOG_LEVEL_EXCEPTION,self.get_msg(msg, *args),need_record_where_it_is)

            def fault(self, msg, *args):
                self.log(self.log_ring, LOG_LEVEL_FAULT,self.get_msg(msg, *args))

        return span_logger(log_ring if log_ring else self.ring[0:1], self._logger)
        
    def _logger(self, log_ring, event, content,need_record_where=False):
        assert(type(log_ring) == list)
            
    def finish(self, finish_time=None,error=False,**kwargs):
        pass
            
